home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d8 / pcparea.arc / PCPAREA.C < prev    next >
Text File  |  1990-04-05  |  5KB  |  207 lines

  1.     /* this program is for those people using Procomm+ that move directories
  2.        from one default area code to another, i.e. from work to home and back.
  3.        If you find this program useful, register your Procomm+ program if you
  4.        have'nt done so already!
  5.  
  6.        If you have any comments or suggestions please contact:
  7.  
  8.        Program author: Albert Stein
  9.        Company:        Software Ideology
  10.                        P.O. Box 305
  11.                        Brooklyn, NY 11204
  12.        Telephone:      1-718-236-3876
  13.  
  14.        Please do not modify and repost this code!  You are free to modify this
  15.        code for your own use only.
  16.  
  17.        Program compiled using TurboC 2.0 on 04/05/90
  18.  
  19.     */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <io.h>
  25. #include <ctype.h>
  26.  
  27. void syntax (void);             /* display command line syntax */
  28. int get_args (char *argv[]);    /* parse/validate command line data */
  29. int process (void);             /* create new .dir file */
  30.  
  31.     /* storage space */
  32. unsigned char old_file[81];
  33. unsigned char new_file[81];
  34. unsigned char old_area[6];
  35. unsigned char new_area[6];
  36.  
  37. void main (int argc, char *argv[])
  38. {
  39.     if (argc != 5)
  40.     {   syntax ();
  41.         exit (1);
  42.     }
  43.     if (get_args (argv) == -1)
  44.         exit (1);
  45.  
  46.     if (process () == -1)
  47.     {   printf ("Original file ok, no new file created!");
  48.         exit (1);
  49.     }
  50.  
  51.     exit (0);
  52. }
  53.  
  54. void syntax ()
  55. {   printf ("Syntax:  PCPAREA old-file new-file old-area new-area\n"
  56.         "   File names default to a .DIR extension if not specified\n"
  57.         "   Area codes must be in the form 1-nnn\n");
  58. }
  59.  
  60.     /* get input from command line,
  61.        verify input for accessibility/validity */
  62. int get_args (char *argv[])
  63. {
  64.     int badform = 0;
  65.     int i;
  66.  
  67.     if (strlen (argv[1]) > 76)
  68.     {   printf ("Input (old) filename to long\n");
  69.         return (-1);
  70.     }
  71.     else
  72.     {   strcpy (old_file,argv[1]);
  73.         if (strchr (old_file, '.') == NULL)
  74.             strcat (old_file,".DIR");
  75.     }
  76.  
  77.     if (access (old_file,0) != 0)
  78.     {   printf ("Unable to access input file '%s'\n",old_file);
  79.         return (-1);
  80.     }
  81.  
  82.     if (strlen (argv[2]) > 76)
  83.     {   printf ("Output (new) filename to long\n");
  84.         return (-1);
  85.     }
  86.     else
  87.     {   strcpy (new_file,argv[2]);
  88.         if (strchr (new_file, '.') == NULL)
  89.             strcat (new_file,".DIR");
  90.     }
  91.  
  92.     if (strlen (argv[3]) != 5)
  93.         badform = 1;
  94.     else
  95.     {   strcpy (old_area,argv[3]);
  96.         if (strncmp (old_area,"1-",2) != 0)
  97.             badform = 1;
  98.         else
  99.         for (i=2; i < 5; i++)   /* check the area code for numeric data */
  100.         {   if (!isdigit (old_area[i]))
  101.             {   badform = 1;
  102.                 break;
  103.             }
  104.         }
  105.     }
  106.  
  107.     if (badform)
  108.     {   printf ("Old area code not in '1-nnn' format\n");
  109.         return (-1);
  110.     }
  111.  
  112.     if (strlen (argv[4]) != 5)
  113.         badform = 1;
  114.     else
  115.     {   strcpy (new_area,argv[4]);
  116.         if (strncmp (new_area,"1-",2) != 0)
  117.             badform = 1;
  118.         else
  119.         for (i=2; i < 5; i++)   /* check the area code for numeric data */
  120.         {   if (!isdigit (new_area[i]))
  121.             {   badform = 1;
  122.                 break;
  123.             }
  124.         }
  125.     }
  126.  
  127.     if (badform)
  128.     {   printf ("New area code not in '1-nnn' format\n");
  129.         return (-1);
  130.     }
  131.  
  132.     strupr (old_file);
  133.     strupr (new_file);
  134.     return (0);
  135. }
  136.  
  137. int process()
  138. {   
  139.     FILE *in, *out;
  140.     unsigned char entry[75];
  141.     unsigned char header[251];
  142.     unsigned char number[22];
  143.     int ret = 0;
  144.     int old = 0, new = 0;
  145.     int i;
  146.  
  147.     if (( in = fopen (old_file,"rb")) == NULL)
  148.     {   printf ("Open failure for old file '%s'\n",old_file);
  149.         return (-1);
  150.     }
  151.  
  152.     if (( out = fopen (new_file,"wb")) == NULL)
  153.     {   printf ("Open failure for new file '%s'\n",new_file);
  154.         fclose (in);
  155.         return (-1);
  156.     }
  157.  
  158.     if ( fread (header,250,1,in) != 1)
  159.     {   printf ("Bad read of header\n");
  160.         ret = -1;
  161.     }
  162.     else
  163.     if ( fwrite (header,250,1,out) != 1)
  164.     {   printf ("Bad write of header\n");
  165.         ret = -1;
  166.     }
  167.  
  168.     for (i=0; 1 ; i++)  /* after header, keep looping thru rest of input */
  169.     {   
  170.         if ( fread (entry,74,1,in) != 1)
  171.         {   if (i < 200)
  172.             {   printf ("Bad read of entry %d\n",i+1);
  173.                 ret = -1;
  174.             }
  175.             break;
  176.         }
  177.         if (strncmp (&entry[25],old_area,5) == 0)   /* found old, strip area */
  178.         {   strcpy (number,&entry[31]);
  179.             sprintf (&entry[25],"%s%c%c%c%c%c%c",number,0,0,0,0,0,0);
  180.             old++;
  181.         }
  182.         else
  183.         if (entry[28] == '-')   /* found no area code, insert new area */
  184.         {   strcpy (number,&entry[25]);
  185.             sprintf (&entry[25],"%s-%s",new_area,number);
  186.             new++;
  187.         }
  188.         if ( fwrite (entry,74,1,out) != 1)
  189.         {   printf ("Bad write of entry %d\n",i+1);
  190.             ret = -1;
  191.             break;
  192.         }
  193.     }
  194.  
  195.     fclose (in);
  196.     fclose (out);
  197.     if (ret == -1)
  198.         unlink (new_file);
  199.     else
  200.         printf ("Successfully updated %d records\n"
  201.                 "%d %s entries changed to nulls\n"
  202.                 "%d null entries changed to %s\n",
  203.                     i,old,old_area,new,new_area);
  204.  
  205.     return (ret);
  206. }
  207.